package cn.mw.cmdb.controller;

import cn.mw.cmdb.entity.ModelSpecificationInfo;
import cn.mw.cmdb.entity.ModelVendorInfo;
import cn.mw.cmdb.entity.MwMacOuiInfo;
import cn.mw.cmdb.enums.OperatorTypeDescEnum;
import cn.mw.cmdb.param.*;
import cn.mw.cmdb.service.ServiceAction;
import cn.mw.cmdb.service.impl.CmdbServiceManage;
import cn.mw.cmdb.service.impl.ModelMacOuiServiceImpl;
import cn.mw.cmdb.service.impl.ModelSpecificationServiceImpl;
import cn.mw.cmdb.service.impl.ModelVendorServiceImpl;
import cn.mw.cmdb.view.ViewManager;
import cn.mw.log.client.LogClientSimple;
import cn.mw.log.enums.BusinessLogType;
import cn.mw.log.enums.LogLevel;
import cn.mw.microMonitorCommon.api.common.BaseApiService;
import cn.mw.microMonitorCommon.api.common.ResponseBase;
import cn.mw.microMonitorCommon.constant.Constants;
import cn.mw.microMonitorCommon.security.utils.SecurityUtils;
import cn.mw.microMonitorCommon.util.DateUtils;
import cn.mw.microMonitorCommon.util.TreeUtils;
import cn.mw.microMonitorCommon.utils.DatabaseUtils;
import com.google.common.base.Strings;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static cn.mw.cmdb.enums.PageDescEnum.*;

@RequestMapping("/modelVendor")
@Controller
@Tag(name = "厂商管理")
@Slf4j
public class ModelVendorController extends BaseApiService {
    private CmdbServiceManage cmdbServiceManage;
    private final MessageSource messageSource;
    private ViewManager viewManager;
    private LogClientSimple logClientSimple;

    public ModelVendorController(CmdbServiceManage cmdbServiceManage, MessageSource messageSource,
                                 ViewManager viewManager, LogClientSimple logClientSimple) {
        this.cmdbServiceManage = cmdbServiceManage;
        this.messageSource = messageSource;
        this.viewManager = viewManager;
        this.logClientSimple = logClientSimple;
    }

    @PostMapping("/addVendor")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('307')")
    @Operation(summary = "新增厂商")
    public ResponseBase createVendor(@RequestBody ModelVendorInfo info) throws Exception {
        cmdbServiceManage.service(info, ServiceAction.add);
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.add.getCname() + ModelManufacture.getDesc(),
                        OperatorTypeDescEnum.add.getCname() + info.getVendorCN(), Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));

        return setResultSuccess(messageSource.getMessage("add-success", null, LocaleContextHolder.getLocale()));
    }

    @PostMapping("/updateVendor")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('308')")
    @Operation(summary = "修改厂商")
    public ResponseBase updateVendor(@RequestBody ModelVendorInfo info) throws Exception {
        cmdbServiceManage.service(info, ServiceAction.update);
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.update.getCname() + ModelManufacture.getDesc(),
                        OperatorTypeDescEnum.update.getCname() + info.getVendorCN(), Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));

        return setResultSuccess(messageSource.getMessage("upd-success", null, LocaleContextHolder.getLocale()));
    }

    @PostMapping("/queryVendorList")
    @ResponseBody
    @Operation(summary = "查询所有厂商列表")
    public ResponseBase queryVendorList(@RequestBody VendorSearchParam info) throws Exception {
        List list = (List) cmdbServiceManage.service(info, ServiceAction.list);
        TreeUtils.sortTree(list, DatabaseUtils.getFieldName(ModelVendorInfo::getVendorCN), DatabaseUtils.getFieldName(ModelVendorInfo::getVendorCN));

        return setResultSuccess(list);
    }

    @PostMapping("/queryVendorById")
    @ResponseBody
    @Operation(summary = "查询指定厂商信息")
    public ResponseBase queryVendorById(@RequestBody ModelVendorInfo info) throws Exception {
        Object o = cmdbServiceManage.service(info, ServiceAction.select);
        return setResultSuccess(o);
    }

    @PostMapping("/deleteVendorById")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('309')")
    @Operation(summary = "删除厂商")
    public ResponseBase deleteVendorById(@RequestBody ModelVendorInfo info) throws Exception {
        ModelVendorServiceImpl vendorService = (ModelVendorServiceImpl) cmdbServiceManage.getModelService(ModelVendorServiceImpl.ID);
        //记录操作日志
        ModelVendorInfo vendorInfo = vendorService.doSelectById(info);
        cmdbServiceManage.service(info, ServiceAction.remove);

        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.remove.getCname() + ModelManufacture.getDesc(),
                        OperatorTypeDescEnum.remove.getCname() + vendorInfo.getVendorCN(), Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));

        return setResultSuccess(messageSource.getMessage("del-success", null, LocaleContextHolder.getLocale()));
    }

    @PostMapping("/batchDeleteVendor")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('309')")
    @Operation(summary = "批量删除厂商")
    public ResponseBase batchDeleteVendor(@RequestBody VendorIdsParam info) throws Exception {
        ModelVendorServiceImpl vendorService = (ModelVendorServiceImpl) cmdbServiceManage.getModelService(ModelVendorServiceImpl.ID);
        //记录操作日志
        List<ModelVendorInfo> infos = vendorService.doSelectByIds(info.getIds());
        cmdbServiceManage.service(info, ServiceAction.batchRemove);
        List<String> collect = infos.stream().map(s -> s.getVendorUS()).collect(Collectors.toList());
        String name = String.join(",", collect);
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.batchRemove.getCname() + ModelManufacture.getDesc(),
                        OperatorTypeDescEnum.remove.getCname() + name, Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));

        return setResultSuccess(messageSource.getMessage("del-success", null, LocaleContextHolder.getLocale()));
    }

    @PostMapping("/queryVendorByTemplate")
    @ResponseBody
    @Operation(summary = "根据模板查询厂商")
    public ResponseBase queryVendorByTemplate(@RequestParam(required = false) String templateId) throws Exception {
        ModelVendorServiceImpl vendorService = (ModelVendorServiceImpl) cmdbServiceManage.getModelService(ModelVendorServiceImpl.ID);
        List<ModelVendorInfo> list = vendorService.queryVendorByTemplate(templateId);
        return setResultSuccess(list);
    }

    @PostMapping("/importVendorByExcel")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('310')")
    @Operation(summary = "导入Excel厂商数据")
    public void importVendorByExcel(@RequestBody MultipartFile file, HttpServletResponse response) throws Exception {
        ModelVendorServiceImpl modelVendorService = (ModelVendorServiceImpl) cmdbServiceManage.getModelService(ModelVendorServiceImpl.ID);
        List<VendorExcelImportParam> vendorExcelImportParams = modelVendorService.importVendorByExcel(file, response);


        //没有错误信息，表示导入全部成功
        String message = "";
        if (CollectionUtils.isNotEmpty(vendorExcelImportParams)) {
            for (int x = 0; x < vendorExcelImportParams.size(); x++) {
                if (!Strings.isNullOrEmpty(vendorExcelImportParams.get(x).getErrorMsg())) {
                    message += "第" + (x + 1) + "行" + vendorExcelImportParams.get(x).getErrorMsg() + ";";
                }
            }
        } else {
            message = "导入成功";
        }
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.batchImport.getCname() + ModelManufacture.getDesc(),
                        OperatorTypeDescEnum.batchImport.getCname() + message, Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));

    }

    @PostMapping("/exportVendorByExcel")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('311')")
    @Operation(summary = "导出Excel厂商数据")
    public void exportVendorByExcel(@RequestBody VendorSearchParam param, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelVendorServiceImpl modelVendorService = (ModelVendorServiceImpl) cmdbServiceManage.getModelService(ModelVendorServiceImpl.ID);
        int num = modelVendorService.exportVendorByExcel(param, request, response);
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.batchExport.getCname() + ModelManufacture.getDesc(),
                        OperatorTypeDescEnum.batchExport.getCname() + num + "条资产数据", Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));

    }

    @PostMapping("/exportVendorTemplate")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('312')")
    @Operation(summary = "导出厂商下载模板")
    public void exportVendorTemplate(@RequestBody VendorSearchParam param, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelVendorServiceImpl modelVendorService = (ModelVendorServiceImpl) cmdbServiceManage.getModelService(ModelVendorServiceImpl.ID);
        param.setExportTemp(true);
        modelVendorService.exportVendorByExcel(param, request, response);
    }

    @PostMapping("/uploadIcon")
    @ResponseBody
    @Operation(summary = "上传图标")
    public ResponseBase uploadIcon(@RequestParam(value = "file") MultipartFile multipartFile) throws Exception {
        ModelVendorServiceImpl modelVendorService = (ModelVendorServiceImpl) cmdbServiceManage.getModelService(ModelVendorServiceImpl.ID);
        String fileName = modelVendorService.uploadIcon(multipartFile);
        return setResultSuccess(fileName);
    }

    @PostMapping("/checkVendorName")
    @ResponseBody
    @Operation(summary = "厂商名称重复性查询")
    public ResponseBase checkVendorName(@RequestBody ModelVendorInfo info) throws Exception {
        ModelVendorServiceImpl modelVendorService = (ModelVendorServiceImpl) cmdbServiceManage.getModelService(ModelVendorServiceImpl.ID);
        boolean isCheck = modelVendorService.checkVendorName(info);
        return setResultSuccess(isCheck);
    }


    @PostMapping("/addSpecification")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('313')")
    @Operation(summary = "新增规格型号")
    public ResponseBase addSpecification(@RequestBody ModelSpecificationInfo info) throws Exception {
        cmdbServiceManage.service(info, ServiceAction.add);
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.add.getCname() + ModelSpecification.getDesc(),
                        OperatorTypeDescEnum.add.getCname() + info.getSpecification(), Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));

        return setResultSuccess(messageSource.getMessage("add-success", null, LocaleContextHolder.getLocale()));
    }

    @PostMapping("/updateSpecification")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('314')")
    @Operation(summary = "修改规格型号")
    public ResponseBase updateSpecification(@RequestBody ModelSpecificationInfo info) throws Exception {
        cmdbServiceManage.service(info, ServiceAction.update);
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.update.getCname() + ModelSpecification.getDesc(),
                        OperatorTypeDescEnum.update.getCname() + info.getSpecification(), Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));

        return setResultSuccess(messageSource.getMessage("upd-success", null, LocaleContextHolder.getLocale()));
    }

    @PostMapping("/querySpecificationList")
    @ResponseBody
    @Operation(summary = "查询规格型号列表")
    public ResponseBase querySpecificationList(@RequestBody SpecificationSearchParam info) throws Exception {
        List list = (List) cmdbServiceManage.service(info, ServiceAction.list);
        ModelSpecificationServiceImpl modelSpecificationService = (ModelSpecificationServiceImpl) cmdbServiceManage.getModelService(ModelSpecificationServiceImpl.ID);
        List lists = modelSpecificationService.dataListConvert(list);
        Object obj = viewManager.getListViewObject(lists, ModelSpecificationInfo.class, info, null);
        return setResultSuccess(obj);
    }

    @PostMapping("/querySpecificationByTempAndBrand")
    @ResponseBody
    @Operation(summary = "根据模板厂商查询规格型号")
    public ResponseBase querySpecificationByTempAndBrand(@RequestBody QuerySpecificationParam info) throws Exception {
        ModelSpecificationServiceImpl modelSpecificationService = (ModelSpecificationServiceImpl) cmdbServiceManage.getModelService(ModelSpecificationServiceImpl.ID);
        List<ModelSpecificationInfo> list = modelSpecificationService.querySpecificationByTempAndBrand(info);
        return setResultSuccess(list);
    }


    @PostMapping("/querySpecificationById")
    @ResponseBody
    @Operation(summary = "查询指定规格型号信息")
    public ResponseBase querySpecificationById(@RequestBody ModelSpecificationInfo info) throws Exception {
        Object o = cmdbServiceManage.service(info, ServiceAction.select);
        return setResultSuccess(o);
    }

    @PostMapping("/checkSpecNameByVendor")
    @ResponseBody
    @Operation(summary = "同一厂商下规格型号名称重复性校验")
    public ResponseBase checkSpecNameByVendor(@RequestBody ModelSpecificationInfo info) throws Exception {
        ModelSpecificationServiceImpl modelSpecificationService = (ModelSpecificationServiceImpl) cmdbServiceManage.getModelService(ModelSpecificationServiceImpl.ID);
        boolean isCheck = modelSpecificationService.checkSpecificationName(info);
        return setResultSuccess(isCheck);
    }

    @PostMapping("/deleteSpecificationById")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('315')")
    @Operation(summary = "删除规格型号")
    public ResponseBase deleteSpecificationById(@RequestBody ModelSpecificationInfo info) throws Exception {
        cmdbServiceManage.service(info, ServiceAction.remove);
        ModelSpecificationServiceImpl specificationService = (ModelSpecificationServiceImpl) cmdbServiceManage.getModelService(ModelSpecificationServiceImpl.ID);
        //记录操作日志
        ModelSpecificationInfo modelSpecificationInfo = specificationService.doSelectById(info.getId());
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.remove.getCname() + ModelSpecification.getDesc(),
                        OperatorTypeDescEnum.remove.getCname() + modelSpecificationInfo.getSpecification(), Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));

        return setResultSuccess(messageSource.getMessage("del-success", null, LocaleContextHolder.getLocale()));
    }

    @PostMapping("/batchDelSpecification")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('315')")
    @Operation(summary = "批量删除规格型号")
    public ResponseBase batchDelSpecification(@RequestBody SpecificationIdsParam info) throws Exception {
        ModelSpecificationServiceImpl modelSpecificationService = (ModelSpecificationServiceImpl) cmdbServiceManage.getModelService(ModelSpecificationServiceImpl.ID);

        List<ModelSpecificationInfo> infos = modelSpecificationService.doSelectByIds(info.getIds());
        cmdbServiceManage.service(info, ServiceAction.batchRemove);
        List<String> collect = infos.stream().map(s -> s.getSpecification()).collect(Collectors.toList());
        String name = String.join(",", collect);
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.remove.getCname() + ModelSpecification.getDesc(),
                        OperatorTypeDescEnum.remove.getCname() + name, Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));

        return setResultSuccess(messageSource.getMessage("del-success", null, LocaleContextHolder.getLocale()));
    }

    @PostMapping("/importSpecificationByExcel")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('316')")
    @Operation(summary = "导入Excel规格型号数据")
    public void importSpecificationByExcel(@RequestBody MultipartFile file, HttpServletResponse response) throws Exception {
        ModelSpecificationServiceImpl modelSpecificationService = (ModelSpecificationServiceImpl) cmdbServiceManage.getModelService(ModelSpecificationServiceImpl.ID);
        List<SpecificationExcelImportParam> specificationExcelImportParams = modelSpecificationService.importSpecificationByExcel(file, response);
        //没有错误信息，表示导入全部成功
        String message = "";
        if (CollectionUtils.isNotEmpty(specificationExcelImportParams)) {
            for (int x = 0; x < specificationExcelImportParams.size(); x++) {
                if (!Strings.isNullOrEmpty(specificationExcelImportParams.get(x).getErrorMsg())) {
                    message += "第" + (x + 1) + "行" + specificationExcelImportParams.get(x).getErrorMsg() + ";";
                }
            }
        } else {
            message = "导入成功";
        }
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.batchImport.getCname() + ModelSpecification.getDesc(),
                        OperatorTypeDescEnum.batchImport.getCname() + message, Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));

    }

    @PostMapping("/exportSpecificationByExcel")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('317')")
    @Operation(summary = "导出Excel规格型号数据")
    public void exportSpecificationByExcel(@RequestBody SpecificationSearchParam param, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelSpecificationServiceImpl modelSpecificationService = (ModelSpecificationServiceImpl) cmdbServiceManage.getModelService(ModelSpecificationServiceImpl.ID);
        int num = modelSpecificationService.exportSpecificationByExcel(param, request, response);
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.batchExport.getCname() + ModelSpecification.getDesc(),
                        OperatorTypeDescEnum.batchExport.getCname() + num + "条资产数据", Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));

    }

    @PostMapping("/exportSpecificationTemplate")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('318')")
    @Operation(summary = "导出规格型号下载模板")
    public ResponseBase exportSpecificationTemplate(@RequestBody SpecificationSearchParam param, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelSpecificationServiceImpl modelSpecificationService = (ModelSpecificationServiceImpl) cmdbServiceManage.getModelService(ModelSpecificationServiceImpl.ID);
        param.setExportTemp(true);
        modelSpecificationService.exportSpecificationByExcel(param, request, response);
        return setResultSuccess(messageSource.getMessage("export-success", null, LocaleContextHolder.getLocale()));
    }

    @PostMapping("/addMacInfo")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('319')")
    @Operation(summary = "新增MAC特征")
    public ResponseBase addMacInfo(@RequestBody MwMacOuiInfo info) throws Exception {
        cmdbServiceManage.service(info, ServiceAction.add);
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.add.getCname() + ModelMAC.getDesc(),
                        OperatorTypeDescEnum.add.getCname() + info.getMac(), Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));

        return setResultSuccess(messageSource.getMessage("add-success", null, LocaleContextHolder.getLocale()));
    }

    @PostMapping("/updateMacInfo")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('320')")
    @Operation(summary = "修改MAC特征")
    public ResponseBase updateMacInfo(@RequestBody MwMacOuiInfo info) throws Exception {
        cmdbServiceManage.service(info, ServiceAction.update);
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.update.getCname() + ModelMAC.getDesc(),
                        OperatorTypeDescEnum.update.getCname() + info.getMac(), Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));

        return setResultSuccess(messageSource.getMessage("upd-success", null, LocaleContextHolder.getLocale()));
    }

    @PostMapping("/queryMacInfoList")
    @ResponseBody
    @Operation(summary = "查询MAC特征列表")
    public ResponseBase queryMacInfoList(@RequestBody MacOuiSearchParam info) throws Exception {
        List list = (List) cmdbServiceManage.service(info, ServiceAction.list);
        Object obj = viewManager.getListViewObject(list, MwMacOuiInfo.class, info, null);
        return setResultSuccess(obj);
    }

    @PostMapping("/queryMacInfoById")
    @ResponseBody
    @Operation(summary = "查询指定MAC特征信息")
    public ResponseBase queryMacInfoById(@RequestBody MwMacOuiInfo info) throws Exception {
        Object o = cmdbServiceManage.service(info, ServiceAction.select);
        return setResultSuccess(o);
    }

    @PostMapping("/getMacVendorByShortName")
    @ResponseBody
    @Operation(summary = "根据厂商简称查询所有全称")
    public ResponseBase getMacVendorByShortName(@RequestBody MwMacOuiInfo info) throws Exception {
        ModelMacOuiServiceImpl modelMacOuiService = (ModelMacOuiServiceImpl) cmdbServiceManage.getModelService(ModelMacOuiServiceImpl.ID);
        List<String> list = modelMacOuiService.getMacVendorByShortName(info);
        return setResultSuccess(list);
    }

    @PostMapping("/checkMacOuiName")
    @ResponseBody
    @Operation(summary = "MacOui名称重复校验")
    public ResponseBase checkMacOuiName(@RequestBody MwMacOuiInfo info) throws Exception {
        ModelMacOuiServiceImpl modelMacOuiService = (ModelMacOuiServiceImpl) cmdbServiceManage.getModelService(ModelMacOuiServiceImpl.ID);
        boolean isCheck = modelMacOuiService.checkMacOuiName(info);
        return setResultSuccess(isCheck);
    }

    @PostMapping("/deleteMacInfoById")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('321')")
    @Operation(summary = "删除MAC特征信息")
    public ResponseBase deleteMacInfoById(@RequestBody MwMacOuiInfo info) throws Exception {
        ModelMacOuiServiceImpl modelMacOuiService = (ModelMacOuiServiceImpl) cmdbServiceManage.getModelService(ModelMacOuiServiceImpl.ID);
        MwMacOuiInfo infos = modelMacOuiService.doSelectById(info.getId());
        cmdbServiceManage.service(info, ServiceAction.remove);
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.remove.getCname() + ModelMAC.getDesc(),
                        OperatorTypeDescEnum.remove.getCname() + infos.getMac(), Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));

        return setResultSuccess(messageSource.getMessage("del-success", null, LocaleContextHolder.getLocale()));
    }

    @PostMapping("/batchDelMacInfo")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('321')")
    @Operation(summary = "批量删除MAC特征信息")
    public ResponseBase batchDelMacInfo(@RequestBody MacOuiIdsParam info) throws Exception {
        ModelMacOuiServiceImpl modelMacOuiService = (ModelMacOuiServiceImpl) cmdbServiceManage.getModelService(ModelMacOuiServiceImpl.ID);
        List<MwMacOuiInfo> infos = modelMacOuiService.doSelectByIds(info.getIds());
        cmdbServiceManage.service(info, ServiceAction.batchRemove);
        List<String> collect = infos.stream().map(s -> s.getMac()).collect(Collectors.toList());
        String name = String.join(",", collect);
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.remove.getCname() + ModelMAC.getDesc(),
                        OperatorTypeDescEnum.remove.getCname() + name, Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));

        return setResultSuccess(messageSource.getMessage("del-success", null, LocaleContextHolder.getLocale()));
    }

    @PostMapping("/importMacOuiByExcel")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('322')")
    @Operation(summary = "导入ExcelMAC特征数据")
    public void importMacOuiByExcel(@RequestBody MultipartFile file, HttpServletResponse response) throws Exception {
        ModelMacOuiServiceImpl modelMacOuiService = (ModelMacOuiServiceImpl) cmdbServiceManage.getModelService(ModelMacOuiServiceImpl.ID);
        List<MacOuiExcelImportParam> macOuiExcelImportParams = modelMacOuiService.importMacOuiByExcel(file, response);
        //没有错误信息，表示导入全部成功
        String message = "";
        if (CollectionUtils.isNotEmpty(macOuiExcelImportParams)) {
            for (int x = 0; x < macOuiExcelImportParams.size(); x++) {
                if (!Strings.isNullOrEmpty(macOuiExcelImportParams.get(x).getErrorMsg())) {
                    message += "第" + (x + 1) + "行" + macOuiExcelImportParams.get(x).getErrorMsg() + ";";
                }
            }
        } else {
            message = "导入成功";
        }
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.batchImport.getCname() + ModelMAC.getDesc(),
                        OperatorTypeDescEnum.batchImport.getCname() + message, Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));

    }

    @PostMapping("/exportMacOuiByExcel")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('323')")
    @Operation(summary = "导出ExcelMAC特征数据")
    public ResponseBase exportMacOuiByExcel(@RequestBody MacOuiSearchParam param, HttpServletResponse response) throws Exception {
        ModelMacOuiServiceImpl modelMacOuiService = (ModelMacOuiServiceImpl) cmdbServiceManage.getModelService(ModelMacOuiServiceImpl.ID);
        int num = modelMacOuiService.exportMacOuiByExcel(param, response);
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.batchExport.getCname() + ModelMAC.getDesc(),
                        OperatorTypeDescEnum.batchExport.getCname() + num + "条资产数据", Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));


        return setResultSuccess(messageSource.getMessage("export-success", null, LocaleContextHolder.getLocale()));
    }

    @PostMapping("/exportMacOuiTemplate")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('324')")
    @Operation(summary = "导出MAC特征下载模板")
    public ResponseBase exportMacOuiTemplate(@RequestBody MacOuiSearchParam param, HttpServletResponse response) throws Exception {
        param.setExportTemp(true);
        ModelMacOuiServiceImpl modelMacOuiService = (ModelMacOuiServiceImpl) cmdbServiceManage.getModelService(ModelMacOuiServiceImpl.ID);
        modelMacOuiService.exportMacOuiByExcel(param, response);
        return setResultSuccess(messageSource.getMessage("export-success", null, LocaleContextHolder.getLocale()));
    }

    @PostMapping("/queryVendorByTemplateName")
    @ResponseBody
    @Operation(summary = "根据模板名称查询厂商")
    public ResponseBase queryVendorByTemplateName(@RequestBody QueryModelTemplateParam param) throws Exception {
        List<ModelVendorInfo> list = new ArrayList<>();
        ModelVendorServiceImpl vendorService = (ModelVendorServiceImpl) cmdbServiceManage.getModelService(ModelVendorServiceImpl.ID);
        if (!Strings.isNullOrEmpty(param.getTemplateName())) {
            list = vendorService.queryVendorByTemplateName(param.getTemplateName());
        }
        return setResultSuccess(list);
    }


}
